package com.itheima.reggie.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;


@RequestMapping("dish")
@Slf4j
@RestController
public class DishController {
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private DishService dishService;
    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto){
        log.info(dishDto.toString());
        dishService.saveWithFlavor(dishDto);
        return R.success("新增菜品成功");
    }
    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("page")
    public R<Page<DishDto>> page(Integer page,Integer pageSize,String name){
        //构造分页构造器对象
        Page<Dish> pageInfo1 = new Page<>(page, pageSize);
        //条件构造器
        QueryWrapper<Dish> queryWrapper = new QueryWrapper<>();
        //添加过滤条件
        queryWrapper.like(StringUtils.isNotBlank(name),"name",name);
        //添加排序条件
        queryWrapper.orderByDesc("update_time");
        //执行分页查询
        dishService.page(pageInfo1,queryWrapper);
        //对象拷贝
        Page<DishDto> pageInfo2 = new Page<>(page, pageSize);
        //拷贝第一部分数据
        List<DishDto> dishDtoList = new ArrayList<>();
        for (Dish dish : pageInfo1.getRecords()) {

            DishDto dishDto = new DishDto();
            //把dish的属性复制给dishDto
            BeanUtils.copyProperties(dish,dishDto);
            //根据id查询分类对象
            Category category = categoryService.getById(dish.getCategoryId());
            //设置分类名称
            dishDto.setCategoryName(category.getName());
            //将dishDtoList添加到集合中
            dishDtoList.add(dishDto);
        }
            pageInfo2.setRecords(dishDtoList);
            //拷贝第二部分数据
            BeanUtils.copyProperties(pageInfo1,pageInfo2,"records");
            return  R.success(pageInfo2);
      }

    /**
     * 根据id查询菜品信息和对应的口味信息
     * @param id
     * @return
     */
      @GetMapping("{id}")
      public R<DishDto> getById(@PathVariable Long id){
          DishDto dishDto = dishService.getByIdWithFlavor(id);
          return R.success(dishDto);
      }

    /**
     * 修改菜品
     * @param dishDto
     * @return
     */
      @PutMapping
      public R<String> update (@RequestBody DishDto dishDto){
          dishService.updateWithFlavor(dishDto);
          return R.success("修改成功");
      }
    @GetMapping("list")
    public  R<List<DishDto>> list(Dish dish){
        //构造查询条件

        QueryWrapper<Dish> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq(dish.getCategoryId() != null, "category_id", dish.getCategoryId());
        queryWrapper1.eq(StringUtils.isNotBlank(dish.getName()), "name", dish.getName());
        queryWrapper1.eq("status", 1);
        queryWrapper1.orderByAsc("sort").orderByAsc("update_time");
        //查询列表
        List<Dish> dishList = dishService.list(queryWrapper1);
       //将每个dish对象转换为dishdto,放入dishdtolist集合中
        List<DishDto> dishDtoList = new ArrayList<>();
        for (Dish dish1 : dishList) {
            DishDto dishDto = new DishDto();
            //拷贝属性
            BeanUtils.copyProperties(dish1,dishDto);
             //根据菜品id查询对应的口味集合
            QueryWrapper<DishFlavor> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("dish_id",dish1.getId());
            List<DishFlavor> dishFlavorList = dishFlavorService.list(queryWrapper2);
            //设置菜品口味的数据
            dishDto.setFlavors(dishFlavorList);
            dishDtoList.add(dishDto);
        }
        return R.success(dishDtoList);
    }
}